// pages/yes/yes.js
const http = require('../../api/index')
const request = require('../../utils/request')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    introduce: '',
    contacts: '',
    phone: '',
    fstate: null,
    showtype: false,
    camptype: '',
    camptypelist: ['综合营地', '微型营地', '帐篷营地', '教育营地', '运动营地'],
    campAddr: null,
    showadress: false,
    baseURL: request.baseURL,
    //弹出层
    price: '250',
    checked: true,
    //步骤条
    active: 0,
    //初始为第一页
    viewshow: 1,
    //步骤条
    steps: [{
        text: '营地信息'
      },
      {
        text: '服务设施'
      }
    ],
    campImgs: [], //商家信息图片(运营者)
    tourisCampImgs: [], //游客图片(非运营者)
    facility: [],
    facilityList: [],
    timeType: 1, //营业时间类型 1全年，2月份
    time: "", //营业时间
    columns: [{
        values: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"], //月份
        className: 'column1',
        defaultIndex: 0,
      },
      {
        values: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"], //月份
        className: 'column2',
        defaultIndex: 2,
      },
    ],

  },
  //上一步
  btnPre() {
    var newActive = --this.data.active;
    var newViewshow = --this.data.viewshow;
    if (newActive >= 0) {
      this.setData({
        active: newActive,
        viewshow: newViewshow
      })
    }
  },
  //下一步
  btnNext() {
    var newActive = ++this.data.active;
    var newViewshow = ++this.data.viewshow;
    if (newActive >= 2) {
      wx.showToast({
          icon: 'none',
          title: '这就是最后一页！！',
          duration: 1500
        })
        --this.data.active;
      --this.data.viewshow;
    } else {
      this.setData({
        active: newActive,
        viewshow: newViewshow
      })
    }
  },
  handleSubmit() {
    console.log('on submit');
  },
  //弹出层的显示与关闭
  showPopup() {
    this.setData({
      showtype: true
    });
  },
  onClose() {
    this.setData({
      showtype: false,
      camptype: null
    });
  },
  onCamptypeChange(event) {
    const { value } = event.detail;
    this.setData({
      camptype: value
    });
  },
  onConfirm() {
    this.setData({
      showtype: false,
    });
  },
  //单选按钮点击
  onClick(event) {
    const { name } = event.currentTarget.dataset;
    this.setData({
      camptype: name,
    });
  },
  //跳转到地图组件
  tomap() {
    wx.navigateTo({
      url: '/components/map/map',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.setData({
      campAddr: options.addr,
      addrName: options.name,
      latitude: options.latitude,
      longitude: options.longitude
    })
    this.getfacility();
  },
  //获取图片
  getUploadList(l) {
    console.log(l)
    this.setData({
      campImgs: l.detail,
      tourisCampImgs: l.detail
    })
  },
  //删除图片
  delImgs(u) {
    this.data.campImgs.splice(u.currentTarget.dataset.index, 1)
    this.setData({
      campImgs: this.data.campImgs
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  // 获取设施列表
  getfacility() {
    http.facilityTypeList().then((res) => {
      if (res.code === 200) {
        
        this.setData({
          facility: res.rows,
        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },
  // 选中服务设施
  selectIcon(e) {
    let a, b, select, list, id, data
    list = this.data.facility
    id = e.currentTarget.dataset.id
    data = this.data.facilityList
    for (let i = 0; i < list.length; i++) {
      for (let j = 0; j < list[i].facilities.length; j++) {
        if (id == list[i].facilities[j].fid) {
          a = i
          b = j
        }
      }
    }
    select = list[a].facilities[b].state || 1
    // 如果没有选中，则选中
    if (select == 1) {
      list[a].facilities[b].state = 2
      data.push(id)
      this.setData({
        facility: list,
        facilityList: data
      })
    } else {
      // 已经选中，则取消选中
      list[a].facilities[b].state = 1
      let index = data.indexOf(id)
      data.splice(index, 1)
      this.setData({
        facility: list,
        facilityList: data
      })
    }

    console.log(this.data.facilityList);
  },
  
  // 商家提交数据
  async businessSub() {
    await this.subCamp()
    await this.subService()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  
  //营业时间
  selectTime() {
    this.setData({
      open: false,
      timeOpen: true,
      time: "全年"
    })
  },
  onChange(e) {
    this.setData({
      timeType: e.detail
    })
    if (e.detail == 1) {
      this.setData({
        timeDetailOpen: false,
        time: "全年"
      })
    } else {
      this.setData({
        timeDetailOpen: true
      })
    }
  },
  onTimeChange(e) {
    console.log(e)
    this.setData({
      time: e.detail.value.join("-")
    })
  },
  // 关闭时间
  closeTime() {
    this.setData({
      timeOpen: false
    })
  },
  // 确定时间
  sureTime() {
    this.setData({
      timeOpen: false
    })
    console.log(this.data.time)
  },
  
  // 返回营地类型id
  campType(value) {
    return this.data.camptypelist.indexOf(value) + 1
  },
  
  // 上传服务设施
  async subService() {
    let res = await http.addService({
      campId: this.data.campId,
      facilityIds: this.data.facilityList
    })
    console.log(res)
    if (res.code == 200) {
      wx.redirectTo({
        url: `../detail/detail?campId=${this.data.campId}`,
      })
    }
  },
  // 提交营地信息
  async subCamp() {
    let userInfo = wx.getStorageSync("userInfo")
    let camptype = this.campType(this.data.camptype)
    let res = await http.addCamp({
      userid: userInfo.userId,
      usertype: 1,
      userType: this.data.userType,
      camptype,
      campname: this.data.name,
      longitude: this.data.longitude, //经度
      latitude: this.data.latitude,
      location: this.data.campAddr,
      address: this.data.addrName,
      timeType: this.data.timeType,
      specificTime: this.data.time,
      linkman: this.data.contacts,
      phone: this.data.phone,
      imageUrl: this.data.campImgs.join(","),
      introduce: this.data.introduce,
      state: 1,
      campUser: {
        userId: userInfo.userId
      }
    })
    this.setData({
      campId: res.msg
    })
  },

  // 营地信息表单校验
  showToast(title) {
    wx.showToast({
      title: title,
      icon: "error",
      mask: true,
      duration: 1500
    })
  },
})